package org.example.infra.service.impl;

import jakarta.annotation.Resource;
import org.example.infra.entity.DataEntity;
import org.example.infra.mapper.IotDbMapper;
import org.example.infra.service.IotDbMybatisService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Description: iotdb 服务
 * Created by 陈宇 on 2024-12-25 16:45
 * Created with IntelliJ IDEA.
 * @author 陈宇
 */
@Service
public class IotDbMybatisServiceImpl implements IotDbMybatisService {

    @Resource
    private IotDbMapper iotDbMapper;

    @Override
    public long getCodeLine(String repoId, String date) {
        List<DataEntity> codeLine = iotDbMapper.getCodeLine(repoId);
        return codeLine.stream().filter(d -> d.getCodeLineDate().equals(date)).collect(Collectors.toList()).get(0).getCodeLine();
    }

    @Override
    public int getForkCount(String repoId, long nextMillis) {
        List<DataEntity> dataEntities = iotDbMapper.queryForkCount(repoId, nextMillis);
        Set<Long> collect = dataEntities.stream().map(data -> data.getRepoForksCount()).collect(Collectors.toSet());
        Long[] array = collect.toArray(new Long[collect.size()]);
        return Integer.parseInt(array[0].toString());
    }

    @Override
    public int getStarCount(String repoId, long nextMillis) {
        List<DataEntity> dataEntities = iotDbMapper.queryStarCount(repoId, nextMillis);
        Set<Long> collect = dataEntities.stream().map(data -> data.getRepoStargazersCount()).collect(Collectors.toSet());
        Long[] array = collect.toArray(new Long[collect.size()]);
        return Integer.parseInt(array[0].toString());
    }

    @Override
    public int getPrCount(String repoId, long nextMillis) {
        List<DataEntity> dataEntities = iotDbMapper.queryPrIds(repoId, nextMillis);
        return dataEntities.stream().map(data -> data.getPullReviewId()).collect(Collectors.toSet()).size();
    }

    @Override
    public int getIssueCount(String repoId, long nextMillis) {
        List<DataEntity> dataEntities = iotDbMapper.queryIssueIds(repoId, nextMillis);
        return dataEntities.stream().map(data -> data.getIssueId()).collect(Collectors.toSet()).size();
    }

    @Override
    public int getContributorCount(String repoId, long nextMillis) {
        List<DataEntity> dataEntities = iotDbMapper.queryContributors(repoId, nextMillis);
        return dataEntities.stream().map(data -> data.getActorId()).collect(Collectors.toSet()).size();
    }

    @Override
    public String queryIssueCommentBody(Long issueCommentId) {
        return iotDbMapper.queryIssueCommentBody(issueCommentId).get(0).getBody();
    }

    @Override
    public String queryPrCommentBody(Long prCommentId) {
        return iotDbMapper.queryPrCommentBody(prCommentId).get(0).getBody();
    }

    @Override
    public List<DataEntity> queryIssues(String repoId, String userId, long currentMillis, long nextMillis) {
        return iotDbMapper.queryIssues(Long.valueOf(repoId), Long.valueOf(userId), currentMillis, nextMillis);
    }

    @Override
    public List<DataEntity> queryPRs(String repoId, String userId, long currentMillis, long nextMillis) {
        return iotDbMapper.queryPRs(Long.valueOf(repoId), Long.valueOf(userId), currentMillis, nextMillis);
    }

    @Override
    public Integer selectCount(String deviceId) {
        return iotDbMapper.selectCount(deviceId);
    }

    @Override
    public List<DataEntity> queryRepoName(String repoId) {
        return this.iotDbMapper.queryRepoName(repoId);
    }

}
